home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / PHPUnit / Assert.php next >
Encoding:
PHP Script  |  2004-10-01  |  8.2 KB  |  323 lines

  1. <?php
  2. //
  3. // +------------------------------------------------------------------------+
  4. // | PEAR :: PHPUnit                                                        |
  5. // +------------------------------------------------------------------------+
  6. // | Copyright (c) 2002-2003 Sebastian Bergmann <sb@sebastian-bergmann.de>. |
  7. // +------------------------------------------------------------------------+
  8. // | This source file is subject to version 3.00 of the PHP License,        |
  9. // | that is available at http://www.php.net/license/3_0.txt.               |
  10. // | If you did not receive a copy of the PHP license and are unable to     |
  11. // | obtain it through the world-wide-web, please send a note to            |
  12. // | license@php.net so we can mail you a copy immediately.                 |
  13. // +------------------------------------------------------------------------+
  14. //
  15. // $Id: Assert.php,v 1.19 2004/09/28 08:01:48 sebastian Exp $
  16. //
  17.  
  18. /**
  19.  * A set of assert methods.
  20.  *
  21.  * @author      Sebastian Bergmann <sb@sebastian-bergmann.de>
  22.  * @copyright   Copyright © 2002-2004 Sebastian Bergmann <sb@sebastian-bergmann.de>
  23.  * @license     http://www.php.net/license/3_0.txt The PHP License, Version 3.0
  24.  * @category    PHP
  25.  * @package     PHPUnit
  26.  */
  27. class PHPUnit_Assert {
  28.     /**
  29.     * @var    boolean
  30.     * @access private
  31.     */
  32.     var $_looselyTyped = FALSE;
  33.  
  34.     /**
  35.     * Asserts that a haystack contains a needle.
  36.     *
  37.     * @param  mixed
  38.     * @param  mixed
  39.     * @param  string
  40.     * @access public
  41.     * @since  1.1.0
  42.     */
  43.     function assertContains($needle, $haystack, $message = '') {
  44.         if (is_string($needle) && is_string($haystack)) {
  45.             $this->assertTrue(strpos($haystack, $needle) !== FALSE ? TRUE : FALSE);
  46.         }
  47.  
  48.         else if (is_array($haystack) && !is_object($needle)) {
  49.             $this->assertTrue(in_array($needle, $haystack), $message);
  50.         }
  51.  
  52.         else {
  53.             $this->fail('Unsupported parameter passed to assertContains().');
  54.         }
  55.     }
  56.  
  57.     /**
  58.     * Asserts that a haystack does not contain a needle.
  59.     *
  60.     * @param  mixed
  61.     * @param  mixed
  62.     * @param  string
  63.     * @access public
  64.     * @since  1.1.0
  65.     */
  66.     function assertNotContains($needle, $haystack, $message = '') {
  67.         if (is_string($needle) && is_string($haystack)) {
  68.             $this->assertFalse(strpos($haystack, $needle) !== FALSE ? TRUE : FALSE);
  69.         }
  70.  
  71.         else if (is_array($haystack) && !is_object($needle)) {
  72.             $this->assertFalse(in_array($needle, $haystack), $message);
  73.         }
  74.  
  75.         else {
  76.             $this->fail('Unsupported parameter passed to assertNotContains().');
  77.         }
  78.     }
  79.  
  80.     /**
  81.     * Asserts that two variables are equal.
  82.     *
  83.     * @param  mixed
  84.     * @param  mixed
  85.     * @param  string
  86.     * @param  mixed
  87.     * @access public
  88.     */
  89.     function assertEquals($expected, $actual, $message = '', $delta = 0) {
  90.         if ((is_array($actual)  && is_array($expected)) ||
  91.             (is_object($actual) && is_object($expected))) {
  92.             if (is_array($actual) && is_array($expected)) {
  93.                 ksort($actual);
  94.                 ksort($expected);
  95.             }
  96.  
  97.             if ($this->_looselyTyped) {
  98.                 $actual   = $this->_convertToString($actual);
  99.                 $expected = $this->_convertToString($expected);
  100.             }
  101.  
  102.             $actual   = serialize($actual);
  103.             $expected = serialize($expected);
  104.  
  105.             $message = sprintf(
  106.               '%sexpected %s, actual %s',
  107.  
  108.               !empty($message) ? $message . ' ' : '',
  109.               $expected,
  110.               $actual
  111.             );
  112.  
  113.             if ($actual !== $expected) {
  114.                 return $this->fail($message);
  115.             }
  116.         }
  117.  
  118.         elseif (is_numeric($actual) && is_numeric($expected)) {
  119.             $message = sprintf(
  120.               '%sexpected %s%s, actual %s',
  121.  
  122.               !empty($message) ? $message . ' ' : '',
  123.               $expected,
  124.               ($delta != 0) ? ('+/- ' . $delta) : '',
  125.               $actual
  126.             );
  127.  
  128.             if (!($actual >= ($expected - $delta) && $actual <= ($expected + $delta))) {
  129.                 return $this->fail($message);
  130.             }
  131.         }
  132.  
  133.         else {
  134.             $message = sprintf(
  135.               '%sexpected %s, actual %s',
  136.  
  137.               !empty($message) ? $message . ' ' : '',
  138.               $expected,
  139.               $actual
  140.             );
  141.  
  142.             if ($actual !== $expected) {
  143.                 return $this->fail($message);
  144.             }
  145.         }
  146.     }
  147.  
  148.     /**
  149.     * Asserts that an object isn't null.
  150.     *
  151.     * @param  object
  152.     * @param  string
  153.     * @access public
  154.     */
  155.     function assertNotNull($object, $message = '') {
  156.         $message = sprintf(
  157.           '%sexpected NOT NULL, actual NULL',
  158.  
  159.           !empty($message) ? $message . ' ' : ''
  160.         );
  161.  
  162.         if ($object === NULL) {
  163.             return $this->fail($message);
  164.         }
  165.     }
  166.  
  167.     /**
  168.     * Asserts that an object is null.
  169.     *
  170.     * @param  object
  171.     * @param  string
  172.     * @access public
  173.     */
  174.     function assertNull($object, $message = '') {
  175.         $message = sprintf(
  176.           '%sexpected NULL, actual NOT NULL',
  177.  
  178.           !empty($message) ? $message . ' ' : ''
  179.         );
  180.  
  181.         if ($object !== NULL) {
  182.             return $this->fail($message);
  183.         }
  184.     }
  185.  
  186.     /**
  187.     * Asserts that a condition is true.
  188.     *
  189.     * @param  boolean
  190.     * @param  string
  191.     * @access public
  192.     */
  193.     function assertTrue($condition, $message = '') {
  194.         $message = sprintf(
  195.           '%sexpected TRUE, actual FALSE',
  196.  
  197.           !empty($message) ? $message . ' ' : ''
  198.         );
  199.  
  200.         if (!$condition) {
  201.             return $this->fail($message);
  202.         }
  203.     }
  204.  
  205.     /**
  206.     * Asserts that a condition is false.
  207.     *
  208.     * @param  boolean
  209.     * @param  string
  210.     * @access public
  211.     */
  212.     function assertFalse($condition, $message = '') {
  213.         $message = sprintf(
  214.           '%sexpected FALSE, actual TRUE',
  215.  
  216.           !empty($message) ? $message . ' ' : ''
  217.         );
  218.  
  219.         if ($condition) {
  220.             return $this->fail($message);
  221.         }
  222.     }
  223.  
  224.     /**
  225.     * Asserts that a string matches a given regular expression.
  226.     *
  227.     * @param  string
  228.     * @param  string
  229.     * @param  string
  230.     * @access public
  231.     */
  232.     function assertRegExp($pattern, $string, $message = '') {
  233.         $message = sprintf(
  234.           '%s"%s" does not match pattern "%s"',
  235.  
  236.           !empty($message) ? $message . ' ' : '',
  237.           $string,
  238.           $pattern
  239.         );
  240.  
  241.         if (!preg_match($pattern, $string)) {
  242.             return $this->fail($message);
  243.         }
  244.     }
  245.  
  246.     /**
  247.     * Asserts that a string does not match a given regular expression.
  248.     *
  249.     * @param  string
  250.     * @param  string
  251.     * @param  string
  252.     * @access public
  253.     * @since  1.1.0
  254.     */
  255.     function assertNotRegExp($pattern, $string, $message = '') {
  256.         $message = sprintf(
  257.           '%s"%s" matches pattern "%s"',
  258.  
  259.           !empty($message) ? $message . ' ' : '',
  260.           $string,
  261.           $pattern
  262.         );
  263.  
  264.         if (preg_match($pattern, $string)) {
  265.             return $this->fail($message);
  266.         }
  267.     }
  268.  
  269.     /**
  270.     * Asserts that a variable is of a given type.
  271.     *
  272.     * @param  string          $expected
  273.     * @param  mixed           $actual
  274.     * @param  optional string $message
  275.     * @access public
  276.     */
  277.     function assertType($expected, $actual, $message = '') {
  278.         return $this->assertEquals(
  279.           $expected,
  280.           gettype($actual),
  281.           $message
  282.         );
  283.     }
  284.  
  285.     /**
  286.     * Converts a value to a string.
  287.     *
  288.     * @param  mixed   $value
  289.     * @access private
  290.     */
  291.     function _convertToString($value) {
  292.         foreach ($value as $k => $v) {
  293.             if (is_array($v)) {
  294.                 $value[$k] = $this->_convertToString($value[$k]);
  295.             } else {
  296.                 settype($value[$k], 'string');
  297.             }
  298.         }
  299.  
  300.         return $value;
  301.     }
  302.  
  303.     /**
  304.     * @param  boolean $looselyTyped
  305.     * @access public
  306.     */
  307.     function setLooselyTyped($looselyTyped) {
  308.         if (is_bool($looselyTyped)) {
  309.             $this->_looselyTyped = $looselyTyped;
  310.         }
  311.     }
  312.  
  313.     /**
  314.     * Fails a test with the given message.
  315.     *
  316.     * @param  string
  317.     * @access protected
  318.     * @abstract
  319.     */
  320.     function fail($message = '') { /* abstract */ }
  321. }
  322. ?>
  323.